home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cracking 2
/
Cracking II..iso
/
Tools
/
ApiHooks 3.0
/
ApiHooksDLL.bat
< prev
next >
Wrap
DOS Batch File
|
2000-06-18
|
21KB
|
562 lines
;@goto translate
.586P
.MODEL FLAT, STDCALL
OPTION CASEMAP: NONE
INCLUDE WINDOWS.inc
UNICODE = FALSE
INCLUDE APIMACRO.mac
INCLUDE NtStatus.inc
INCLUDE AHinc.inc
INCLUDE ApiHooks.inc
INCLUDE NtStruc.inc
INCLUDELIB iKERNEL32.lib
INCLUDELIB iUSER32.lib
INCLUDELIB iADVAPI32.lib
;------------------------------------------------------------------------------
.CODE
ASSUME FS: NOTHING
INCLUDE ApiWorks.inc
TEXTA zSE_DEBUG_NAME, <SeDebugPrivilege/0>
INCLUDE ModWorks.inc
;place for APIs which were retrieved via GetProcAddress
;initialzed with names of APIs for saving image size
ALIGN 4
VirtualAllocEx LABEL DWORD
K32 BYTE 'KERN'
sK32 EQU OFFSET K32
VirtualFreeEx LABEL DWORD
BYTE 'EL32'
CreateToolhelp32Snapshot LABEL DWORD
BYTE '.DLL'
NtQueryInformationProcess LABEL DWORD
BYTE 0
CT32S BYTE 'Cre'
sCT32S EQU OFFSET CT32S
NtQuerySystemInformation LABEL DWORD
BYTE 'ateT'
RtlCreateUserThread LABEL DWORD
BYTE 'oolh'
NtQueryInformationThread LABEL DWORD
BYTE 'elp3'
Thread32First LABEL DWORD
BYTE '2Sna'
Thread32Next LABEL DWORD
BYTE 'psho'
NtOpenThread LABEL DWORD
BYTE 't',0
T32F BYTE 'Th'
sT32F EQU OFFSET T32F
W32Version LABEL DWORD
BYTE 'read'
CurrentProcess LABEL DWORD
BYTE '32Fi'
CurPID LABEL DWORD
BYTE 'rst',0
RemoteAlloc DWORD RemoteAlloc9x
RemoteExec DWORD RemoteExec9x
RemoteFree DWORD RemoteFree9x
OpenAllThreads DWORD OpenAllThreads9x
;-----------------------------------------
;For NT user thread termination is used NtTerminateThread not ExitThread,
;because KERNEL32.dll may not be present in the target process, while
;NTDLL.dll is present everywhere. For more comfort I could prepend
;LdrShutdownThread.
NTThreadExit:
PUSHp EAX, CurrentThread, EAX ; 4
BYTE 68H ;PUSH DWORD ;+1
NtTerminateThread DWORD ? ;+4
RET ;+1
NTThreadExitSize EQU ($-NTThreadExit) ; 10 ~ 12 = movsd,movsd,movsd
;-----------------------------------------
;names of NT APIs for GetProcAddress
TEXTA VAEX, <VirtualAllocEx/0>
TEXTA VFEX, <VirtualFreeEx/0>
TEXTA NOPT, <NtOpenThread/0>
TEXTA NQIP, <NtQueryInformationProcess/0>
TEXTA NQSI, <NtQuerySystemInformation/0>
TEXTA RCUT, <RtlCreateUserThread/0>
TEXTA NTTH, <NtTerminateThread/0>
TEXTA NQIT, <NtQueryInformationThread/0>
;=================================================================================
DllMain PROC
CMP DWORD PTR [ESP+8], DLL_PROCESS_ATTACH ;only this is important
JNE DllMainRet
iWin32 DisableThreadLibraryCalls, [ESP+4] ;don't bother with DLL_THREAD_*
PUSHp ESI, EDI ;save used registers
;--------------
iWin32i GetModuleHandle, sK32 ;initialize KERNEL32 APIs
iMOV ESI, GetProcAddress
MOV EDI, EAX
sWin32 ESI, EDI, sVAEX
MOV VirtualAllocEx, EAX
sWin32 ESI, EDI, sVFEX
MOV VirtualFreeEx, EAX
sWin32 ESI, EDI, sCT32S
MOV CreateToolhelp32Snapshot, EAX
sWin32 ESI, EDI, sT32F
MOV Thread32First, EAX
sWin32 ESI, EDI, sT32N
MOV Thread32Next, EAX
iWin32i GetModuleHandle, sNTDLL ;initialize NTDLL APIs
MOV EDI, EAX
sWin32 ESI, EDI, sNOPT
MOV NtOpenThread, EAX
sWin32 ESI, EDI, sNQIP
MOV NtQueryInformationProcess, EAX
sWin32 ESI, EDI, sNQSI
MOV NtQuerySystemInformation, EAX
sWin32 ESI, EDI, sRCUT
MOV RtlCreateUserThread, EAX
sWin32 ESI, EDI, sNTTH
MOV NtTerminateThread, EAX
sWin32 ESI, EDI, sNQIT
MOV NtQueryInformationThread, EAX
;--------------------
;enable debug privilege for this process if possible
iWin32 GetCurrentProcess
PUSH ECX ;place for hToken
MOV CurrentProcess, EAX
iWin32 OpenProcessToken, EAX, TOKEN_QUERY OR TOKEN_ADJUST_PRIVILEGES, ESP
TEST EAX, EAX
POP EDI ;hToken
JE CantOpenToken ;failed
;LookupPrivValue can be excluded if I use hard ntddk value for debug priv.: 14H in LUID
iWin32i LookupPrivilegeValue, NULL, szSE_DEBUG_NAME, OFFSET dbLUID
TEST EAX, EAX
JE CloseToken ;can't find local (numeric) representation of the privilege
;enable the privilege in my token
iWin32 AdjustTokenPrivileges, EDI, FALSE, OFFSET NewState, 0, NULL, NULL
CloseToken:
iWin32 CloseHandle, EDI ;close hToken
CantOpenToken:
;--------------------
;initialze APIs for remote codes (ApiWorks and ModWorks)
iMOV EAX, VirtualQuery
MOV _VirtualQuery, EAX
iMOV EAX, GetModuleFileNameA
MOV _GetModuleFileNameA, EAX
iMOV EAX, LoadLibraryA
MOV _LoadLibraryA, EAX
MOV _GetProcAddress, ESI ;in ApiWorks
MOV _GetProcAddr, ESI ;in ModWorks
iMOV EAX, GetModuleHandleA
MOV _GetModuleHandleA, EAX
MOV _DllOperation, EAX
iMOV EAX, VirtualProtect
MOV _VirtualProtect, EAX
iMOV EAX, lstrcmpiA
MOV _lstrcmpiA, EAX
iMOV EAX, KERNEL32_ORD_0001
MOV _KERNEL32_ORD_0001, EAX
iMOV EAX, FreeLibrary
MOV _FreeLibrary, EAX
;--------------------
iWin32 GetCurrentProcessId
MOV CurPID, EAX ;my PID
XOR EAX, FS:TEB.pPEB ;9x Obsfucator = MyPID ^ TEB.pProcess
MOV Obsfucator, EAX
;--------------------
iWin32 GetVersion
MOV W32Version, EAX
TEST EAX, EAX
JNS DoNT
DoW9x: ;9x stuff
iMOV EAX, OpenProcess
ADD EAX, 24H
CMP DWORD PTR [EAX], 000000B9H ;is OpenThread routine present?
JNE NoOpenThread9x
MOV W9xOpenThread, EAX
JMP Initialized
DoNT: ;NT stuff
MOV BYTE PTR W9xJMP0, 75H ;allow NO_UNBIND in ApiWorks: JMP -> JNE
MOV RemoteAlloc, RemoteAllocNT
MOV RemoteFree, RemoteFreeNT
MOV OpenAllThreads,OpenAllThreadsNT
CMP AL, 5 ;Win2K+ -> don't patch thread info start
JAE Initialized
MOV ThreadInfoStart, NT4_SYSTEM_PROCESS_INFORMATION.ThreadInformation + SYSTEM_THREAD_INFORMATION.ClientId.UniqueThread
;--------------------
NoOpenThread9x:
Initialized:
POPc ESI, EDI ;restore used registers
;--------------------
DllMainRet:
XOR EAX, EAX
INC EAX ;return TRUE
RET 12
DllMain ENDP
;=================================================================================
;Exception handler sets EIP to DrWtson, ESP to xFrameESP and EAX to error code
xHandler PROC
MOV EDX, [ESP+12] ;context
ADD EDX, CONTEXT.regEax
MOV EAX, [ESP+8] ;xESP
MOV [EDX+CONTEXT.regEip-CONTEXT.regEax], DrWatson
MOV [EDX+CONTEXT.regEsp-CONTEXT.regEax], EAX
oMOV [EDX+CONTEXT.regEax-CONTEXT.regEax], ErrorException
XOR EAX, EAX ;ExceptionContinueExecution
RETN
xHandler ENDP
;=================================================================================
;Checks if process represented by procID is initialzed. In 9x it is always
;true. In NT it is not true if the process was created suspended and wasn't
;resumed yet. Remote thread in such a process in Win2K = crash the process.
;Returns FALSE if process is not initialize